package com.xl98.controller;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xl98.entity.*;
import com.xl98.service.StateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ls
 * @since 2020-05-15
 */
@RestController
@RequestMapping("/api/state")
@CrossOrigin
@Api(value = "状态",tags = {"状态"})
public class StateController {
    @Autowired
    private StateService stateService;
    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @ResponseBody
    @RequestMapping(value = "/all", method = RequestMethod.POST)
    @ApiOperation(value = "查询所有状态")
    public Result findAll ()  {
        List<State> stateList = stateService.findAllState();
        if(StringUtils.checkValNotNull(stateList)){
            return ResultFactory.buidResult(ResponseCode.SUCCESS_CODE,ResponseCode.SUCCESS_FIND,stateList);
        }
        else{
            return ResultFactory.buildFailResult(ResponseCode.FAIL_FIND);
        }
    }

    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @ResponseBody
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ApiOperation(value = "添加状态")
    public Result insert(@RequestBody State state){
        return ResultFactory.buildSuccessResult(stateService.insert(state));
    }

    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "修改状态")
    public Result update(@RequestBody State state){
        return ResultFactory.buildSuccessResult(stateService.update(state));
    }

    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiOperation(value = "删除状态")
    public Result delete(@RequestBody String id){
        return ResultFactory.buildSuccessResult(stateService.delete(id));
    }

    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @ResponseBody
    @RequestMapping(value = "/updateAppointmentState", method = RequestMethod.POST)
    @ApiOperation(value = "修改预约状态")
    public Result updateAppointmentState(String appointmentId, String stateId){
        return ResultFactory.buildSuccessResult(stateService.updateAppointmentState(appointmentId,stateId));
    }

    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @ResponseBody
    @RequestMapping(value = "/updateUserState", method = RequestMethod.POST)
    @ApiOperation(value = "修改用户状态")
    public Result updateUserState(String userId, String stateId){
        System.out.println("c"+stateId);
        return ResultFactory.buildSuccessResult(stateService.updateUserState(userId,stateId));
    }
}

